import { useUserStore } from './../stores';
import { storeToRefs } from 'pinia'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({
  showSpinner: false
})
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/index.vue'),meta:{title:'登录'} },
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        { path: '/home', component: () => import('@/views/Home/index.vue') ,meta:{title:'首页'}},
        { path: '/article', component: () => import('@/views/Article/index.vue'),meta:{title:'健康百科'} },
        { path: '/notify', component: () => import('@/views/Notify/index.vue'),meta:{title:'消息通知'} },
        { path: '/user', component: () => import('@/views/User/index.vue'),meta:{title:'我的'} }
      ]
    },
    { path: '/user/patient', component: () => import('@/views/User/patient.vue'),meta:{title:'家庭档案'} },
  ]
})
const whiteList=['/login']
router.beforeEach(to=>{
  NProgress.start()
  const store =  useUserStore()
  if(!store.user?.token&&!whiteList.includes(to.path)) return'/login'
})
// 访问权限控制
router.afterEach((to) => {
  // 处理标题
  document.title = `优医问诊-${to.meta.title || ''}`
  NProgress.done()
})

export default router
